using MediatR;
using Microsoft.AspNetCore.Mvc;
using Medical.ErrorCode;
using BaseData.ErrorCode;
using Medical.Domain;
using Medical.WriteApi.Appcontext.Command;
using Medical.ReadApi.Dto;

namespace Medical.WriteApi.Controllers
{
    /// <summary>
    /// 挂号单管理控制器（写操作）
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ManagementController : ControllerBase
    {
        private readonly IMediator _mediator;

        public ManagementController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 创建挂号单模板
        /// </summary>
        /// <param name="command">创建命令</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<RegistrationSlipTemplateDto>> Create([FromBody] CreateRegistrationSlipTemplateCommand command)
        {
            try
            {
                var result = await _mediator.Send(command);
                return new ApiResult<RegistrationSlipTemplateDto>
                {
                    Code = ApiEnum.新增成功,
                    Message = "创建成功",
                    data = result
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<RegistrationSlipTemplateDto>
                {
                    Code = ApiEnum.新增失败,
                    Message = ex.Message,
                    data = null
                };
            }
        }

        /// <summary>
        /// 更新挂号单模板
        /// </summary>
        /// <param name="command">更新命令</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<RegistrationSlipTemplateDto>> Update([FromBody] UpdateRegistrationSlipTemplateCommand command)
        {
            try
            {
                var result = await _mediator.Send(command);
                return new ApiResult<RegistrationSlipTemplateDto>
                {
                    Code = ApiEnum.更新成功,
                    Message = "更新成功",
                    data = result
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<RegistrationSlipTemplateDto>
                {
                    Code = ApiEnum.更新失败,
                    Message = ex.Message,
                    data = null
                };
            }
        }

        /// <summary>
        /// 删除挂号单模板（逻辑删除）
        /// </summary>
        /// <param name="templateId">模板ID</param>
        /// <param name="deletedBy">删除人</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult<bool>> Delete([FromQuery] long templateId, [FromQuery] string? deletedBy = null)
        {
            try
            {
                var command = new DeleteRegistrationSlipTemplateCommand
                {
                    TemplateId = templateId,
                    DeletedBy = deletedBy
                };
                var result = await _mediator.Send(command);
                return new ApiResult<bool>
                {
                    Code = ApiEnum.删除成功,
                    Message = "删除成功",
                    data = result
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<bool>
                {
                    Code = ApiEnum.删除失败,
                    Message = ex.Message,
                    data = false
                };
            }
        }
    }
}

